"""
Author: MoQsien
License: GPL v2.0
Version: 0.01
Description: Scraping http://www.cnstock.com/
"""
import requests
import json
import pymongo


MONGO_URI = 'localhost'
MONGO_DB = 'zhzhw'
MONGO_COLLECTION = 'zzw'


class ZhongZW(object):
    """中证网股票2017年年报
    """

    def __init__(self):
        self.url = "http://data.cnstock.com/result/gpsj/cwsj/report_1.js"
        self.headers = {
            "Referer": "ttp://data.cnstock.com/gpsj/cwsj/cwsj.html",
            "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.170 Safari/537.36"
        }
        self.client = pymongo.MongoClient(MONGO_URI)
        self.collection = MONGO_COLLECTION

    def __del__(self):
        self.client.close()

    def get_content(self, url):
        respons = requests.get(url, headers=self.headers)
        return respons.content.decode()

    def process_data(self, content):
        json_data = json.loads(content, encoding='utf-8')
        return json_data['rows']

    def save_data(self, datas):
        self.db = self.client[MONGO_DB]
        for i in datas:
            data = i['cell']
            self.db[self.collection].insert(data)

    def run_spider(self):
        content = self.get_content(self.url)
        datas = self.process_data(content)
        self.save_data(datas)


def main():
    zhong = ZhongZW()
    zhong.run_spider()


if __name__ == '__main__':
    main()
